home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / Drop•MPSR / DSUserProcs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-03  |  12.4 KB  |  409 lines  |  [TEXT/KAHL]

  1. /******************************************************************************
  2. **
  3. **  Project Name:    DropShell
  4. **     File Name:    DSUserProcs.c
  5. **
  6. **   Description:    Specific AppleEvent handlers used by the DropBox
  7. **
  8. *******************************************************************************
  9. **                       A U T H O R   I D E N T I T Y
  10. *******************************************************************************
  11. **
  12. **    Initials    Name
  13. **    --------    -----------------------------------------------
  14. **    LDR            Leonard Rosenthol
  15. **    MTC            Marshall Clow
  16. **    SCS            Stephan Somogyi
  17. **
  18. *******************************************************************************
  19. **                      R E V I S I O N   H I S T O R Y
  20. *******************************************************************************
  21. **
  22. **      Date        Time    Author    Description
  23. **    --------    -----    ------    ---------------------------------------------
  24. **    06/23/94            LDR        Added support for ProcessItem and ProcessFolder handling
  25. **    02/20/94            LDR        Modified Preflight & Postflight to take item count
  26. **    01/25/92            LDR        Removed the use of const on the userDataHandle
  27. **    12/09/91            LDR        Added the new SelectFile userProc
  28. **                                Added the new Install & DisposeUserGlobals procs
  29. **                                Modified PostFlight to only autoquit on odoc, not pdoc
  30. **    11/24/91            LDR        Added the userProcs for pdoc handler
  31. **                                Cleaned up the placement of braces
  32. **                                Added the passing of a userDataHandle
  33. **    10/29/91            SCS        Changes for THINK C 5
  34. **    10/28/91            LDR        Officially renamed DropShell (from QuickShell)
  35. **                                Added a bunch of comments for clarification
  36. **    10/06/91    00:02    MTC        Converted to MPW C
  37. **    04/09/91    00:02    LDR        Added to Projector
  38. **
  39. ******************************************************************************/
  40.  
  41. #include <StandardFile.h>
  42.  
  43. #include "DSGlobals.h"
  44. #include "DSUserProcs.h"
  45.  
  46. // Static Prototypes
  47. static OSErr ProcessItem(FSSpecPtr myFSSPtr);
  48. static OSErr ProcessFolder(FSSpecPtr myFSSPtr);
  49.  
  50.  
  51. /*
  52.     Uncomment this line if you want each item of a dropped folder processed
  53.     as an individual item
  54. */
  55. #define qWalkFolders
  56.  
  57.  
  58. /*
  59.     This routine is called during init time.
  60.     
  61.     It allows you to install more AEVT Handlers beyond the standard four
  62. */
  63. #pragma segment Main
  64. pascal void InstallOtherEvents (void) {
  65. }
  66.  
  67.  
  68. /*    
  69.     This routine is called when an OAPP event is received.
  70.     
  71.     Currently, all it does is set the gOApped flag, so you know that
  72.     you were called initally with no docs, and therefore you shouldn't 
  73.     quit when done processing any following odocs.
  74. */
  75. #pragma segment Main
  76. pascal void OpenApp (void) {
  77.     gOApped = true;
  78. }
  79.  
  80.  
  81. /*    
  82.     This routine is called when an QUIT event is received.
  83.     
  84.     We simply set the global done flag so that the main event loop can
  85.     gracefully exit.  We DO NOT call ExitToShell for two reasons:
  86.     1) It is a pretty ugly thing to do, but more importantly
  87.     2) The Apple event manager will get REAL upset!
  88. */
  89. #pragma segment Main
  90. pascal void QuitApp (void) {
  91.     gDone = true;    /*    All Done! */
  92. }
  93.  
  94.  
  95. /*    
  96.     This routine is the first one called when an ODOC or PDOC event is received.
  97.     
  98.     In this routine you would place code used to setup structures, etc. 
  99.     which would be used in a 'for all docs' situation (like "Archive all
  100.     dropped files")
  101.  
  102.     Obviously, the opening boolean tells you whether you should be opening
  103.     or printing these files based on the type of event recieved.
  104.     
  105.     NEW IN 2.0!
  106.     The itemCount parameter is simply the number of items that were dropped on
  107.     the application and that you will be processing.  This gives you the ability
  108.     to do a single preflight for memory allocation needs, rather than doing it
  109.     once for each item as in previous versions.
  110.     
  111.     userDataHandle is a handle that you can create & use to store your own
  112.     data structs.  This dataHandle will be passed around to the other 
  113.     odoc/pdoc routines so that you can get at your data without using
  114.     globals - just like the new StandardFile.  
  115.     
  116.     We also return a boolean to tell the caller if you support this type
  117.     of event.  By default, our dropboxes don't support the pdoc, so when
  118.     opening is FALSE, we return FALSE to let the caller send back the
  119.     proper error code to the AEManager.
  120.  
  121.     You will probably want to remove the #pragma unused (currently there to fool the compiler!)
  122. */
  123. #pragma segment Main
  124. pascal Boolean PreFlightDocs (Boolean opening, short itemCount, Handle *userDataHandle) {
  125. #pragma unused ( itemCount )
  126. #pragma unused ( userDataHandle )
  127.     
  128.     extern void MPSR_Preflight(short count,Handle* dataHdl);
  129.     
  130.     if (opening)// not while printing...
  131.         MPSR_Preflight(itemCount,userDataHandle);
  132.     
  133.     return opening;        // we support opening, but not printing - see above
  134. }
  135.  
  136.  
  137. /*    
  138.     This routine is called for each file passed in the ODOC event.
  139.     
  140.     In this routine you would place code for processing each file/folder/disk that
  141.     was dropped on top of you.
  142.     
  143.     You will probably want to remove the #pragma unused (currently there to fool the compiler!)
  144. */
  145. #pragma segment Main
  146. pascal void OpenDoc ( FSSpecPtr myFSSPtr, Boolean opening, Handle userDataHandle ) {
  147. #pragma unused ( myFSSPtr )
  148. #pragma unused ( opening )
  149. #pragma unused ( userDataHandle )
  150.     OSErr    err = noErr;
  151.     
  152.     
  153.     #ifdef qWalkFolders
  154.     /*
  155.         For this case we need to determine if the FSSpec is a file or folder.
  156.         If it's a folder, we then need to process each item in that folder,
  157.         otherwise just process the item.
  158.     */
  159.     if (FSpIsFolder(myFSSPtr))
  160.         err = ProcessFolder(myFSSPtr);
  161.     else
  162.         err = ProcessItem(myFSSPtr);
  163.     #else
  164.     /*
  165.         For this case we just call ProcessItem on the FSSpec above.
  166.     */
  167.     err = ProcessItem(myFSSPtr);
  168.     #endif
  169.     
  170.     // you should probably do something if you get back an error ;)
  171. }
  172.  
  173.  
  174. /*    
  175.     This routine is the last routine called as part of an ODOC event.
  176.     
  177.     In this routine you would place code to process any structures, etc. 
  178.     that you setup in the PreflightDocs routine.
  179.  
  180.     NEW IN 2.0!
  181.     The itemCount parameter was the number of items that you processed.
  182.     It is passed here just in case you need it ;)  
  183.     
  184.     If you created a userDataHandle in the PreFlightDocs routines, this is
  185.     the place to dispose of it since the Shell will NOT do it for you!
  186.     
  187.     You will probably want to remove the #pragma unusued (currently there to fool the compiler!)
  188. */
  189. #pragma segment Main
  190. pascal void PostFlightDocs ( Boolean opening, short itemCount, Handle userDataHandle ) {
  191. #pragma unused ( opening )
  192. #pragma unused ( itemCount )
  193. #pragma unused ( userDataHandle )
  194.     
  195.     extern void MPSR_Postflight(short count,Handle dataHdl);
  196.     
  197.     if ( (opening) && (!gOApped) )
  198.         gDone = true;    //    close everything up!
  199.  
  200.     /*
  201.         The reason we do not auto quit is based on a recommendation in the
  202.         Apple event Registry which specifically states that you should NOT
  203.         quit on a 'pdoc' as the Finder will send you a 'quit' when it is 
  204.         ready for you to do so.
  205.     */
  206.     
  207.     MPSR_Postflight(itemCount,userDataHandle);
  208. }
  209.  
  210.  
  211. /*
  212.     This routine gets called for each item (which could be either a file or a folder)
  213.     that the caller wants dropped.  The determining factor is the definition of the 
  214.     qWalkFolder compiler directive.   Either way, the item in question should be
  215.     processed as a single item and not "dissected" into component units (like subfiles
  216.     of a folder!)
  217. */
  218. static OSErr ProcessItem(FSSpecPtr myFSSPtr)
  219. {
  220.     extern OSErr MPSR_Process(FSSpecPtr theSpec);
  221.     OSErr    err = noErr;
  222.     
  223.     // do something here
  224.     
  225.     err=MPSR_Process(myFSSPtr);
  226.     
  227.     return(err);
  228. }
  229.  
  230. /*
  231.     This routine gets called for any folder (or disk) that the caller wants 
  232.     processed as a set of component items, instead of as a single entity.
  233.     The determining factor is the definition of the qWalkFolder compiler directive.
  234. */
  235. static OSErr ProcessFolder(FSSpecPtr myFSSPtr)
  236. {
  237.     OSErr        err = noErr;
  238.     short        index, oldIndex, localIndex;
  239.     FSSpec        localFSSpec, curFSSpec;
  240.     CInfoPBRec    cipb;
  241.     Str255        fName, vFName;
  242.     long        dirID, origDirID;
  243.     Boolean        foundPosition;
  244.  
  245.      // copy the source locally to avoid recursion problems
  246.      BlockMoveData(myFSSPtr, &localFSSpec, sizeof(FSSpec));
  247.      
  248.     //    get the dirID for THIS folder, not it's parent!
  249.     BlockMoveData(localFSSpec.name, fName, 32);
  250.     
  251.     cipb.hFileInfo.ioCompletion    = 0L;
  252.     cipb.hFileInfo.ioNamePtr    = fName;
  253.     cipb.hFileInfo.ioVRefNum    = localFSSpec.vRefNum;
  254.     cipb.hFileInfo.ioFDirIndex    = 0;    // use the dir & vRefNum;
  255.     cipb.hFileInfo.ioDirID        = localFSSpec.parID;
  256.     err = PBGetCatInfoSync(&cipb);
  257.     
  258.     if (!err) {        
  259.         origDirID = cipb.dirInfo.ioDrDirID; // copy the sucker
  260.         index = 1;
  261.                 
  262.         // index through all contents of this folder
  263.         while (err == noErr) {
  264.             dirID = origDirID;
  265.             localIndex = index;
  266.             fName [0] = 0;
  267.             cipb.hFileInfo.ioCompletion    = 0L;
  268.             cipb.hFileInfo.ioNamePtr    = fName;
  269.             cipb.hFileInfo.ioVRefNum    = localFSSpec.vRefNum;
  270.             cipb.hFileInfo.ioFDirIndex    = localIndex;    // use a real index
  271.             cipb.hFileInfo.ioDirID        = dirID;
  272.             err = PBGetCatInfoSync(&cipb);
  273.  
  274.             if (!err) {
  275.                 BlockMoveData(fName, curFSSpec.name, 32);
  276.                 curFSSpec.vRefNum    = cipb.hFileInfo.ioVRefNum;
  277.                 curFSSpec.parID        = dirID;
  278.             
  279.                 /*    
  280.                     Check to see if this entry is a folder.
  281.                 */
  282.                 if (cipb.hFileInfo.ioFlAttrib & ioDirMask) {
  283.                     err = ProcessFolder(&curFSSpec);
  284.                  } else
  285.                     err = ProcessItem(&curFSSpec);
  286.             
  287.                 /*    If we've had an error, get out! */
  288.                 if (err)    break;
  289.  
  290.                 // dirID = origDirID;    
  291.                 localIndex = index;    
  292.  
  293.                 /*    
  294.                     Now take into account new files being created
  295.                     in the current directory & messing up our index.
  296.                     See Dev.CD Vol. XI:Tools & Apps (Moof!):Misc Utilities:
  297.                     Disinfectant & Source 2.5.1:Sample:Notes:Scan Alg    
  298.                 */
  299.                 vFName [0] = 0;
  300.                 cipb.hFileInfo.ioCompletion    = 0L;
  301.                 cipb.hFileInfo.ioNamePtr    = vFName;
  302.                 cipb.hFileInfo.ioVRefNum    = localFSSpec.vRefNum;
  303.                 cipb.hFileInfo.ioFDirIndex    = localIndex;    // use a real index
  304.                 cipb.hFileInfo.ioDirID        = dirID;
  305.                 err = PBGetCatInfoSync(&cipb);
  306.                 oldIndex = index;
  307.                 if (!err) {
  308.                     /*    If they're equal - same place, go to next */
  309.                     if (EqualString (vFName, fName, false, false))
  310.                         index++;
  311.                 }
  312.                 
  313.                 /*    If we didn't advance, then perhaps a file was created or deleted */
  314.                 if (oldIndex == index) {
  315.                     oldIndex        = index;    /* save off the old */
  316.                     index            = 0;        /* and start at the beginning */
  317.                     err                = noErr;
  318.                     vFName [0]        = 0;
  319.                     foundPosition    = false;
  320.                     
  321.                     while (!foundPosition) {
  322.                         index++;
  323.                         vFName [0] = 0;
  324.                         cipb.hFileInfo.ioCompletion    = 0L;
  325.                         cipb.hFileInfo.ioNamePtr    = vFName;
  326.                         cipb.hFileInfo.ioVRefNum    = localFSSpec.vRefNum;
  327.                         cipb.hFileInfo.ioFDirIndex    = index;    /* now use a real index */
  328.                         cipb.hFileInfo.ioDirID        = dirID;
  329.                         err = PBGetCatInfoSync(&cipb);
  330.                         
  331.                         if (err == fnfErr) {  // we've just been deleted
  332.                             index = oldIndex;
  333.                             foundPosition = true;
  334.                             err = noErr;    // have to remember to reset this!
  335.                         }
  336.                         
  337.                     /*    found same file & same index position */
  338.                     /*    so try the next item */
  339.                         if ((!foundPosition) && EqualString(fName, vFName, false, false)) {
  340.                             index++;
  341.                             foundPosition = true;
  342.                         }
  343.                     }
  344.                 }
  345.             }
  346.         }
  347.     }
  348.     
  349.     return(err);
  350. }
  351.  
  352. /*
  353.     This routine is called when the user chooses "Select File…" from the
  354.     File Menu.
  355.     
  356.     Currently it simply calls the new StandardGetFile routine to have the
  357.     user select a single file (any type, numTypes = -1) and then calls the
  358.     SendODOCToSelf routine in order to process it.  
  359.             
  360.     The reason we send an odoc to ourselves is two fold: 1) it keeps the code
  361.     cleaner as all file openings go through the same process, and 2) if events
  362.     are ever recordable, the right things happen (this is called Factoring!)
  363.  
  364.     Modification of this routine to only select certain types of files, selection
  365.     of multiple files, and/or handling of folder & disk selection is left 
  366.     as an exercise to the reader.
  367. */
  368. pascal void SelectFile (void)
  369. {
  370.     StandardFileReply    stdReply;
  371.     SFTypeList            theTypeList;
  372.  
  373.     theTypeList[0]='TEXT';
  374.     theTypeList[1]='text';
  375.     
  376.     StandardGetFile(NULL, -1, theTypeList, &stdReply);
  377.     if (stdReply.sfGood)    // user did not cancel
  378.         SendODOCToSelf(&stdReply.sfFile);    // so send me an event!
  379. }
  380.  
  381. /*
  382.     This routine is called during the program's initialization and gives you
  383.     a chance to allocate or initialize any of your own globals that your
  384.     dropbox needs.
  385.     
  386.     You return a boolean value which determines if you were successful.
  387.     Returning false will cause DropShell to exit immediately.
  388. */
  389. pascal Boolean InitUserGlobals(void)
  390. {
  391.     extern Boolean MPSR_Init(void);
  392.     
  393.     return MPSR_Init();
  394.     return(true);    // nothing to do, it we must be successful!
  395. }
  396.  
  397. /*
  398.     This routine is called during the program's cleanup and gives you
  399.     a chance to deallocate any of your own globals that you allocated 
  400.     in the above routine.
  401. */
  402. pascal void DisposeUserGlobals(void)
  403. {
  404.     extern void MPSR_Cleanup(void);
  405.     
  406.     MPSR_Cleanup();
  407.     // nothing to do for our sample dropbox
  408. }
  409.